package stark.app.frame.netframe;

import androidx.lifecycle.MutableLiveData;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import stark.app.api.DemoApi;
import stark.common.basic.lifecycle.BaseViewModel;
import stark.common.basic.utils.LogUtil;

/**
 * <pre>
 *     author : cfans
 *     time   : 2020/10/09
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class DetailViewModel extends BaseViewModel {
    /**
     * 当数据请求成功回调
     */
    protected MutableLiveData<DetailBean> mDetail = new MutableLiveData<>();

    public MutableLiveData<DetailBean> getDetail() {
        return mDetail;
    }

    /**
     * 请求网络数据
     */
    public void requestData() {
        mShowDialog.setValue(true, "加载中");
        LogUtil.e("requestData");
        Disposable disposable = DemoApi.getInstance().biaoqingList(1,10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<DetailBean>() {
                    @Override
                    public void accept(DetailBean bean) throws Exception {
                        LogUtil.e("requestData "+bean);
                        mShowDialog.setValue(false);
                        mDetail.setValue(bean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        LogUtil.e("requestData Error"+throwable.toString()+"\n");

                        mShowDialog.setValue(false);
                        mError.setValue("发生错误了");
                    }
                });
        addDisposable(disposable);
    }

}
